#!/bin/sh

# Slackware build script for Go-oo.org

# Copyright 2006-2009  Robby Workman, Northport, Alabama, ASA
# Copyright 2009  Jasm Sison

# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

PRGNAM=openoffice.org
VERSION=3.0.1
ARCH=x86_64	# The binaries are built for x86_64 - leave this alone
BUILD=${BUILD:-3}
TAG=${TAG:-_SBo}

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

RPMS=RPMS

# This should allow you to either pass a different value of OOLANG when you
# run the script (OOLANG=pt-BR ./openoffice.org.SlackBuild for example) or
# change it in this script.  Note that you will need to have the correct
# tarball in the build directory for this to work...
# You can get localized tarballs from the following location:
# ftp://ftp.free.fr/mirrors/ftp.openoffice.org/localized/$OOLANG/$VERSION/
# Thanks to Niki Kovacs for finding this :-)
OOLANG=${OOLANG:-en-US}
PKG_LANG=$(echo $OOLANG | sed s/-/_/)	# Leave this alone
OOBUILD=9379

FILELIST="ooobasis3.0-base-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-binfilter-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-calc-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-core01-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-core02-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-core03-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-core04-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-core05-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-core06-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-core07-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-draw-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-$OOLANG-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-$OOLANG-base-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-$OOLANG-binfilter-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-$OOLANG-calc-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-$OOLANG-draw-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-$OOLANG-help-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-$OOLANG-impress-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-$OOLANG-math-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-$OOLANG-res-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-$OOLANG-writer-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-gnome-integration-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-graphicfilter-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-images-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-impress-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-javafilter-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-kde-integration-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-math-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-ooofonts-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-ooolinguistic-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-pyuno-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-testtool-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-writer-$VERSION-$OOBUILD.$ARCH.rpm \
ooobasis3.0-xsltfilter-$VERSION-$OOBUILD.$ARCH.rpm \
openoffice.org-ure-1.4.1-$OOBUILD.$ARCH.rpm \
openoffice.org3-$VERSION-$OOBUILD.$ARCH.rpm \
openoffice.org3-base-$VERSION-$OOBUILD.$ARCH.rpm \
openoffice.org3-calc-$VERSION-$OOBUILD.$ARCH.rpm \
openoffice.org3-draw-$VERSION-$OOBUILD.$ARCH.rpm \
openoffice.org3-$OOLANG-$VERSION-$OOBUILD.$ARCH.rpm \
openoffice.org3-impress-$VERSION-$OOBUILD.$ARCH.rpm \
openoffice.org3-math-$VERSION-$OOBUILD.$ARCH.rpm \
openoffice.org3-writer-$VERSION-$OOBUILD.$ARCH.rpm \
openoffice.org3.0-freedesktop-menus-3.0-$OOBUILD.noarch.rpm"

ADDRESS=http://go-oo.mirrorbrain.org/stable/linux-$ARCH/$VERSION

mkdir -p $RPMS
cd $RPMS
for f in $FILELIST
do
  wget -nc $ADDRESS/$f
done
cd -

# Some localized versions only ship with a bundled JRE, and we'd like this
# script to work on those as well...
WJRE=${WJRE:-no}

# If you want to disable java support by removing executable permissions
# form OOo's java loader (this will not affect other apps), set this
# variable to "YES"   Default is "NO"
# According to Fred Emmott, this is *highly* desirable on 64 bit systems
DISABLE_JAVA=${DISABLE_JAVA:-NO}

set -eu

rm -rf $PKG # reset
mkdir -p $TMP $PKG $OUTPUT

cd $RPMS

for FILE in *.rpm ; do rpm2cpio < $FILE | cpio -imdv ; done
mv opt $PKG

# Create symlinks in /usr/bin to actual binaries
mkdir -p $PKG/usr/bin
cd $PKG/usr/bin

for FILE in \
  sbase scalc sdraw simpress smath soffice spadmin swriter unopkg ; do 
  rm -f $FILE
  ln -sf ../../opt/openoffice.org3/program/$FILE $FILE ;
done
cd $PKG

# Remove DejaVu and Liberation fonts - these are included in other packages
rm -f opt/openoffice.org/basis3.0/share/fonts/truetype/[DL]*.ttf

# Fix Exec commands in the desktop files
# See http://bugzilla.xfce.org/show_bug.cgi?id=2430 
patch -p1 < $CWD/desktop-file-fix-3.0.1.diff

# Install dictionary extensions for spellchecking
UNOPKG_BIN=$PKG/opt/openoffice.org3/program/unopkg
DICT_DIR=$PKG/opt/openoffice.org3/share/extension/install
if [ -x "$UNOPKG_BIN" ]; then
  UNOPKG_TMP=$TMP/$$$$$$
  find $DICT_DIR -type f -name "dict-*.oxt" \
    -exec $UNOPKG_BIN add --shared {} \
    "-env:UserInstallation=file:///$UNOPKG_TMP" \;
  rm -rf $UNOPKG_TMP 	# Clean up after ourselves
fi

# Move docs to their expected locations
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cd $PKG/opt/openoffice.org3
  mv README* *LICENSE* readmes licenses $PKG/usr/doc/$PRGNAM-$VERSION
cd -
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
cat $CWD/desktop-file-fix-3.0.1.diff > $PKG/usr/doc/desktop-file-fix-3.0.1.diff

# Disable Java support if desired (see above)
if [ "$DISABLE_JAVA" = "YES" ]; then
  chmod -x $PKG/opt/openoffice.org/ure/bin/javaldx
fi

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

# Fix ownership and permissions and make the package
chown -R root:root .
find . -type d -exec chmod 755 {} \;
chmod -R u+rw,go+r-w,a-s .
/sbin/makepkg -c n -l y $OUTPUT/$PRGNAM-${VERSION}_${PKG_LANG}-$ARCH-$BUILD$TAG.tgz
